package com.jianganming.netty.tcp;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.nio.charset.Charset;
import java.util.UUID;

public class TcpServer {
    public static void main(String[] args) {
        EventLoopGroup group1 = new NioEventLoopGroup(1);
        EventLoopGroup group2 = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        try {
            ChannelFuture future = bootstrap.group(group1, group2)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast(new MessageDecoder());
                            pipeline.addLast(new MessageEncoder());
                            pipeline.addLast(new SimpleChannelInboundHandler<MessageProtocol>() {
                                private int count;
                                @Override
                                protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
                                    /*byte[] bytes = new byte[byteBuf.readableBytes()];
                                    byteBuf.readBytes(bytes);*/
                                    String message = new String(messageProtocol.getContent(), Charset.forName("utf-8"));
                                    System.out.println("服务端收到消息  "+message);
                                    System.out.println("服务端收到次数  "+(++count));

                                    String uuid = UUID.randomUUID().toString();
                                    MessageProtocol messageProtocol1 = new MessageProtocol();
                                    byte[] bytes = uuid.getBytes();
                                    messageProtocol1.setLen(bytes.length);
                                    messageProtocol1.setContent(bytes);
                                    channelHandlerContext.writeAndFlush(messageProtocol1);
                                }

                                @Override
                                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                    cause.printStackTrace();
                                    count=0;
                                    ctx.close();
                                }
                            });
                        }
                    }).bind(8889).sync();

            future.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            group1.shutdownGracefully();
            group2.shutdownGracefully();
        }
    }
}
